<template>
	<div class="alarmConfiguration zy-content">
		<search :columns="searchData" @getSearch="getSearch(arguments)"></search>
		<div class="list-layout mt16">
			<div class="btns clear">
				<span class="title">{{ $t('common.listInformation') }}</span>
				<Button v-has="'wlczCode'" @click="createAdd">{{ $t('common.New') }}</Button>
				<Button @click="handleCommonReject">撤销</Button>
				<Button @click="exportTemplateClick">{{ $t('common.batchExport') }}</Button>
			</div>
			<div class="tableContent">
				<Table
					class="resizable-table"
					border
					@on-column-width-resize="onColumnWidthResize"
					:columns="columnsList"
					ellipsis
					ref="selection"
					stripe
					:data="dataArr"
					@on-selection-change="onSelectChange"
				>
					<template slot-scope="{ row }" slot="action">
						<span class="operationBtn" @click="createfaultCodeShowH(row)"><i class="iconfont zy-common-bianji"></i>{{ $t('common.Edit') }}</span>
						<span class="operationBtn" @click="delPrrShowH(row)"><i class="iconfont zy-common-shanchu"></i>{{ $t('common.Delete') }}</span>
					</template>
				</Table>
			</div>
			<div class="pageWrap mt16">
				<pageLeftTitle :total="total" :selected="delIds.length" />
				<Page
					:total="total"
					:page-size="pageSize"
					:page-size-opts="pageSizeOption"
					:current="pageNum"
					@on-change="changePageNum"
					@on-page-size-change="changePageSize"
					show-sizer
					show-total
				/>
			</div>
		</div>
		<inputModal ref="inputModal"></inputModal>
		<addModal @refresh="searchList" ref="addModal"></addModal>
	</div>
</template>

<script>
import search from '@/components/curd/searchList.vue';
import fileDownload from 'js-file-download';
import toggleTableOperation from '@/mixins/toggleTableOperation';
import inputModal from '@/components/modalTypes/inputModal.vue';
import addModal from './addModal.vue';

export default {
	name: 'materialOperation',
	components: { search, inputModal, addModal },
	mixins: [toggleTableOperation],
	data() {
		return {
			formItem: {},
			addtitle: '',
			searchParams: {},
			pageNum: 1,
			pageSize: 10,
			total: 0,

			searchData: [
				{
					title: '车间',
					key: 'positionCode',
					type: 'cascader',
					dicUrl: '/basic/organization/findListByToken?level=0',
					params: {
						label: 'name',
						value: 'code',
						children: 'children',
					},
				},
				{
					title: '工单号',
					key: 'workCode',
				},
				{
					title: '工单组号',
					key: 'workGroup',
				},
				{
					title: '产品编号',
					key: 'productCode',
				},
				{
					title: '产品名称',
					key: 'productName',
				},
				{
					title: '物料编号',
					key: 'materialCode',
				},
				{
					title: '物料名称',
					key: 'materialName',
				},
				{
					title: '规格型号',
					key: 'specificationModel',
				},
				{
					title: '班次',
					key: 'shift',
				},
				{
					title: '操作时间',
					key: 'time',
					type: 'datetimerange',
					format: 'yyyy-MM-dd HH:mm:ss',
					span: 8,
				},
				// {
				//   title: "操作工",
				//   key: "operator",
				//   dicUrl: "/basic/user/searchUser",
				//   type: "search",
				//   queryName: "key",
				//   resultParams: "res.data",
				//   params: {
				//     label: "realname",
				//     value: "realname",
				//   },
				// },

				{
					title: '操作人',
					key: 'operator',
				},
			],
			columns: [
				{
					key: 'selection',
					type: 'selection',
					width: 60,
					align: 'center',
				},
				// {
				//   title: "物料操作单号",
				//   key: "id",
				//   minWidth: 130,
				//   align: "center",
				//   resizable: true,
				// },
				{
					title: '车间',
					key: 'functionalLocationName',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '班次',
					key: 'shift',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '工单号',
					key: 'workCode',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '工单组号',
					key: 'groupItem',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				{
					title: '产品编号',
					key: 'productNumber',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '产品名称',
					key: 'productName',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '设备编号',
					key: 'deviceCode',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '模具编号',
					key: 'moldCode',
					minWidth: 130,
					align: 'center',
					resizable: true,
				},
				// {
				//   title: "产品型号",
				//   key: "productModel",
				//   minWidth: 150,
				//   align: "center",
				//   resizable: true,
				// },
				// {
				//   title: "计划开始时间",
				//   key: "operatorStartTime",
				//   minWidth: 150,
				//   align: "center",
				//   resizable: true,
				// },
				{
					title: '物料编号',
					key: 'materialCode',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '物料名称',
					key: 'materialCode',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '规格型号',
					key: 'specificationModel',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '数量',
					key: 'quantity',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '单位',
					key: 'unit',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '状态',
					key: 'status',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销时间',
					key: 'revocationTime',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销人',
					key: 'revocationName',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '撤销原因',
					key: 'reasonForRevocation',
					minWidth: 180,
					align: 'center',
					resizable: true,
					tooltip: true,
				},
				{
					title: '操作时间',
					key: 'operatorTime',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
				{
					title: '操作人',
					key: 'operation',
					minWidth: 180,
					align: 'center',
					resizable: true,
				},
			],
			dataArr: [],
			delFaultCodeShowF: false, //是否显示删除弹窗
			pageSizeOption: [10, 20, 30, 40],
			delIds: [],
			selection: [],
		};
	},
	mounted() {
		this.getTableColumnsWidth(this.columns);
		this.searchList();
	},
	methods: {
		onSelectChange(selection) {
			let myArray = [];
			selection.map((item, index) => {
				myArray.push(item);
			});
			this.delIds = myArray;
		},
		getSearch(arg) {
			this.searchParams = JSON.parse(JSON.stringify(arg[0]));
			const { time } = this.searchParams;
			if (time && time.length > 0) {
				this.searchParams.operatorStartTime = time[0];
				this.searchParams.operatorEndTime = time[1];
				delete this.searchParams.time;
			}

			// if (positionCode && positionCode.length !== 0) {
			//   this.searchParams.functionalLocation = [loca.at(-1)];
			// }
			this.pageNum = 1;
			this.searchList();
		},
		handleCommonReject() {
			// if (this.delIds.length !== 1) {
			// 	this.$Message.warning('请选择一条数据进行操作！');
			// 	return;
			// }

			this.$refs.inputModal.showModal({
				title: '撤销',
				submit: (form) => {
					const data = this.delIds.map((item) => {
						return {
							id: item.id,
							reasonForRevocation: form.value,
						};
					});
					this.axios({
						method: 'post',
						url: '/dms/materialHeadTail/revokeMaterialHeadTail',
						data,
					}).then((res) => {
						if (res.code === 200) {
							this.$Message.success(res.msg);
							this.searchList();
						} else {
							this.$Message.warning({
								content: res.msg,
								duration: 6,
								closable: true,
							});
						}
					});
				},
			});
		},
		createAdd() {
			this.$refs.addModal.show();
		},
		reset() {
			this.searchParams = {};
			this.changeNum();
		},
		//分页
		changeNum() {
			this.pageNum = 1;
			this.searchList();
		},
		changePageSize(pageSize) {
			this.pageSize = pageSize;
			this.searchList();
		},

		changePageNum(val) {
			this.pageNum = val;
			this.searchList();
		},
		//查询列表
		searchList() {
			let params = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				...this.searchParams,
			};
			this.axios({
				method: 'post',
				url: '/dms/materialHeadTail/queryMaterialHeadTailList',
				data: params,
			}).then((res) => {
				if (res.code === 200) {
					this.dataArr = res.data.records;
					this.total = res.data.total;
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		//导出
		exportTemplateClick() {
			//   let params = {
			//   };
			//   for (let key in this.searchParams) {
			//       if (this.searchParams[key]) {
			//           this.$set(params, key, this.searchParams[key])
			//       }
			//   }
			const filName = '料头料尾记录';
			this.axios({
				url: '/dms/materialHeadTail/export',
				method: 'post',
				data: {
					...this.searchParams,
				},
				responseType: 'blob',
			}).then((res) => {
				fileDownload(res, filName + '.xls');
			});
			// this.axios({
			// 	url: '/dms/materialHeadTail/export',
			// 	method: 'post',
			// 	data: {
			// 		...this.searchParams,
			// 	},
			// }).then((res) => {
			// 	if (res.code == 555) {
			// 		this.$Message.warning({
			// 			content: res.msg,
			// 			duration: 6,
			// 			closable: true,
			// 		});
			// 	} else {
			// 		this.axios({
			// 			url: '/dms/materialHeadTail/export',
			// 			method: 'post',
			// 			data: {
			// 				...this.searchParams,
			// 			},
			// 			responseType: 'blob',
			// 		}).then((res) => {
			// 			fileDownload(res, filName + '.xls');
			// 		});
			// 	}
			// });
		},
	},
};
</script>

<style scoped lang="scss">
.ivu-input-number {
	width: -webkit-fill-available;
}

.row_click {
	cursor: pointer;
	color: rgb(50, 113, 240) !important;
}
</style>
